import amqp from 'amqplib';

interface EmailTask {
    to: string;
    subject: string;
    body: string;
}

async function sendEmailTask() {
    const connection = await amqp.connect('amqp://mingcai_mq:EUDNfpZRbhfjb8PZ@95.169.25.181');
    const channel = await connection.createChannel();
    const queueName = 'email_tasks';

    // 声明一个邮件任务队列
    await channel.assertQueue(queueName, { durable: true });
    for (let i = 0; i < 2; i++) {
        // 发送邮件任务消息
        const emailTask: EmailTask = {
            to: `recipient${i}@example.com`,
            subject: `Hello RabbitMQ `,
            body: 'This is a test email from RabbitMQ!',
        };
        channel.sendToQueue(queueName, Buffer.from(JSON.stringify(emailTask)), {
            contentType: 'application/json',
            persistent: true,
            timestamp:555,
            appId: "",
            messageId: ""
        });


        console.log('Email task sent:', emailTask);
    }


    // 关闭连接
    setTimeout(() => {
        connection.close();
    }, 500);
}

(async ()=>{

    await  sendEmailTask()
})()
